﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.Web.Configuration;
using System.Web.Services;

public partial class Default2 : System.Web.UI.Page
{
    private static string id ;
    private static string place_id;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if ((Request.QueryString["id"] != null) && (Request.QueryString["id"] != string.Empty))
            {
                id = Request.QueryString["id"].ToString();
                getShopMapFromDataBaseId();
            }
            if ((Request.QueryString["place_id"] != null) && (Request.QueryString["place_id"] != string.Empty))
            {
                place_id = Request.QueryString["place_id"].ToString();
            }
        }
    } 
    public void DeleteRowtoId()
    {
        try
        {
            SqlConnection conn = OpenConnection();
            SqlCommand cmd = new SqlCommand("UPDATE tbShops SET del_flag = 1  WHERE id =" + id, conn);
            cmd.ExecuteNonQuery();
            CloseConnection(conn);
        }
        catch (Exception)
        {

        }
    }
    public void editShopToDataBaseId()
    {
        try
        {
            string updateSql = "UPDATE tbShops " + "SET name = @name,description =  @description,address = @address,tel = @tel,phone = @phone,image = @image" + " WHERE id = @id";
            SqlConnection conn = OpenConnection();
            SqlCommand UpdateCmd = new SqlCommand(updateSql, conn);

            UpdateCmd.Parameters.Add("@id", SqlDbType.Int, 10, "id");
            UpdateCmd.Parameters.Add("@name", SqlDbType.NVarChar, 50, "name");
            UpdateCmd.Parameters.Add("@description", SqlDbType.NVarChar,1000,"description");
            UpdateCmd.Parameters.Add("@address", SqlDbType.NVarChar, 50, "address");
            UpdateCmd.Parameters.Add("@tel", SqlDbType.NVarChar, 50, "tel");
            UpdateCmd.Parameters.Add("@phone", SqlDbType.NVarChar, 50, "phone");
            UpdateCmd.Parameters.Add("@image", SqlDbType.NVarChar, 100, "image");

            UpdateCmd.Parameters["@id"].Value = id;
            UpdateCmd.Parameters["@name"].Value =BoxName.Text;
            UpdateCmd.Parameters["@description"].Value = BoxDes.Text;
            UpdateCmd.Parameters["@address"].Value = BoxAddress.Text;
            UpdateCmd.Parameters["@tel"].Value = BoxTel.Text;
            UpdateCmd.Parameters["@phone"].Value = Boxphone.Text;
            UpdateCmd.Parameters["@image"].Value = Boximage.Text;

            UpdateCmd.ExecuteNonQuery();
            CloseConnection(conn);
        }
        catch (System.Exception ex)
        {
            System.Diagnostics.Debug.WriteLine(ex.Message);
        }
    }
    public void getShopMapFromDataBaseId()
    {
        try
        {
            SqlConnection conn = OpenConnection();
            SqlDataAdapter da = new SqlDataAdapter("Select * from tbShops Where id =" + id, conn);
            DataSet ds = new DataSet();
            da.Fill(ds);
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                BoxName.Text = dr["name"].ToString();
                BoxDes.Text = dr["description"].ToString();
                BoxAddress.Text = dr["address"].ToString();
                BoxTel.Text = dr["tel"].ToString();
                Boxphone.Text = dr["phone"].ToString();
                Boximage.Text = dr["image"].ToString();
            }
            CloseConnection(conn);
        }
        catch (System.Exception ex)
        {
            System.Diagnostics.Debug.WriteLine(ex.Message);
           
        }
    }
    public static SqlConnection OpenConnection()
    {
        try
        {
            ConnectionStringSettings settings = WebConfigurationManager.ConnectionStrings["shop_ConnectionString"];
            SqlConnection connection = new SqlConnection(settings.ConnectionString);
            connection.Open();
            return connection;
        }
        catch (Exception)
        {
            return null;
        }
    }
    public static void CloseConnection(SqlConnection connection)
    {
        if (connection != null)
        {
            connection.Close();
        }
    }
    public int getShopActiveid()
    {
        int count = 0;
        try
        {
            string sql = "SELECT COUNT(del_flag) FROM  tbShops WHERE (del_flag = 0 And place_id = " + place_id + ")";
            SqlConnection conn = OpenConnection();
            SqlCommand cmd = new SqlCommand(sql, conn);
            count = (Int32)cmd.ExecuteScalar();
            CloseConnection(conn);
            return count;
        }
        catch (System.Exception ex)
        {
            return count;
        }
       
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        DeleteRowtoId();
        int active = getShopActiveid();
        ClientScript.RegisterStartupScript(this.GetType(), "close win", "<script>setActive("+place_id+","+ active +");</script>");
    }
    protected void btnOK_Click(object sender, EventArgs e)
    {
        editShopToDataBaseId();
        ClientScript.RegisterStartupScript(this.GetType(), "close win", "<script>updateEdit();</script>");
    }
}